package com.example.translator;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;

public class FileTableModel extends AbstractTableModel {
    private static final String[] COLUMN_NAMES = {"序号", "原文件名", "翻译后文件名", "状态"};
    private final List<FileInfo> fileInfos;

    public FileTableModel() {
        this.fileInfos = new ArrayList<>();
    }

    @Override
    public int getRowCount() {
        return fileInfos.size();
    }

    @Override
    public int getColumnCount() {
        return COLUMN_NAMES.length;
    }

    @Override
    public String getColumnName(int column) {
        return COLUMN_NAMES[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        FileInfo fileInfo = fileInfos.get(rowIndex);
        
        switch (columnIndex) {
            case 0:
                return rowIndex + 1;
            case 1:
                return fileInfo.getFullOriginalName();
            case 2:
                return fileInfo.getTranslatedName();
            case 3:
                if (fileInfo.isTranslationStatus()) {
                    return "已翻译";
                } else if (!fileInfo.getErrorMsg().isEmpty()) {
                    return "翻译失败";
                } else {
                    return "待翻译";
                }
            default:
                return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return Integer.class;
            default:
                return String.class;
        }
    }

    public void addFile(FileInfo fileInfo) {
        fileInfos.add(fileInfo);
        fireTableRowsInserted(fileInfos.size() - 1, fileInfos.size() - 1);
    }

    public void clear() {
        fileInfos.clear();
        fireTableDataChanged();
    }

    public List<FileInfo> getFileInfos() {
        return new ArrayList<>(fileInfos);
    }

    public FileInfo getFileInfoAt(int rowIndex) {
        if (rowIndex >= 0 && rowIndex < fileInfos.size()) {
            return fileInfos.get(rowIndex);
        }
        return null;
    }
}
